package ferp.android.ads;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import com.google.ads.AdSize;
import com.google.ads.mediation.MediationAdRequest;
import com.google.ads.mediation.customevent.CustomEventBanner;
import com.google.ads.mediation.customevent.CustomEventBannerListener;
import com.inneractive.api.ads.InneractiveAd;
import com.inneractive.api.ads.InneractiveAdListener;
import ferp.android.R;
import ferp.core.log.Log;

import java.util.Hashtable;

/**
 * User: igorgok
 * Date: 10/11/12 4:54 PM
 */
public class InneractiveCustomEventBanner implements CustomEventBanner
{
  @Override
  public void requestBannerAd(final CustomEventBannerListener customEventBannerListener, final Activity activity, String s, String s1, AdSize adSize, MediationAdRequest mediationAdRequest, Object o)
  {
    Log.debug(Log.TAG, "INNERACTIVE custom event received");

    final ViewGroup v = (ViewGroup)activity.findViewById(R.id.ad);

    for (int i = 0; i < v.getChildCount(); ++i)
    {
      View child = v.getChildAt(i);

      if (child instanceof InneractiveAd)
      {
        v.removeViewAt(i);
        break;
      }
    }

    final InneractiveAd banner = new InneractiveAd(activity, "Personal_Ferp_Android", InneractiveAd.IaAdType.Banner, -22, metadata);

    v.addView(banner);

    banner.setInneractiveListener(new InneractiveAdListener()
    {
      public void onIaAdReceived()
      {
        Log.debug(Log.TAG, "inneractive AD received");
        v.removeView(banner);
        customEventBannerListener.onReceivedAd(banner);
      }

      public void onIaDefaultAdReceived()
      {
        Log.debug(Log.TAG, "inneractive default AD received");
        v.removeView(banner);
        customEventBannerListener.onReceivedAd(banner);
      }

      public void onIaAdFailed()
      {
        Log.debug(Log.TAG, "inneractive AD failed");

        activity.runOnUiThread(new Runnable()
        {
          @Override
          public void run()
          {
            v.removeView(banner);
            customEventBannerListener.onFailedToReceiveAd();
          }
        });
      }

      public void onIaAdClicked()
      {
        //Log.debug(Log.TAG, "inneractive banner clicked!");
        v.removeView(banner);
        customEventBannerListener.onClick();
      }

      public void onIaAdResize() {
        //Log.debug(Log.TAG, "inneractive resize");
      }

      public void onIaAdResizeClosed() {/* do nothing */}
      public void onIaAdExpand() {/* do nothing */}
      public void onIaAdExpandClosed() {/* do nothing */}

      public void onIaDismissScreen()
      {
        //Log.debug(Log.TAG, "inneractive screen dismissed");
        customEventBannerListener.onDismissScreen();
      }
    });
  }

  @Override
  public void destroy()
  {
    //To change body of implemented methods use File | Settings | File Templates.
  }

  private static final Hashtable<InneractiveAd.IaOptionalParams,String> metadata = new Hashtable<InneractiveAd.IaOptionalParams,String>();

  static
  {
    InneractiveAd.Timeout.setConnectionTimeout(10000);
    InneractiveAd.Timeout.setSocketTimeout(10000);
    //metadata.put(InneractiveAd.IaOptionalParams.Key_OptionalAdHeight, "53");
    //metadata.put(InneractiveAd.IaOptionalParams.Key_OptionalAdWidth, "320");
    //metadata.put(InneractiveAd.IaOptionalParams.Key_RequiredAdHeight, "53");
    metadata.put(InneractiveAd.IaOptionalParams.Key_Alignment, String.valueOf(InneractiveAd.IaAdAlignment.TOP_CENTER));
  }


}
